;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?product=") != -1) {
			var reqPath = basePath+'userManager/queryUserById.action'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var user = res.data;
						$("#uu_id").val(user.uuid);
						$("#user_name").val(user.userName);
						$("#pass_word").val(user.password);
						$("#qq_name").val(user.qqName);
						$("#ph_one").val(user.phone);
						$("#tele_phone").val(user.telephone);
						$("#user_type").val(user.userType);
						$("#user_score").val(user.score);
						if(user.userPower=='1')
						{
							$("#userpower_1").checked=true;
						}else if (user.userPower=='0')
						{
							$("#userpower_0").checked=true;
						}
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else if(url.indexOf("?type=") != -1)
		{
			queryLocation(null);
			queryColor(null);
			queryStuff(null);
			queryStyle(null);
			queryModel(null);
			queryProductType(null);
			queryUse(null);
			queryOpen(null);
		}
		else
		{
			//初始化列表
			initProductData(1,15);
		}
		$('#addnew').click(function()
		{

			window.location.href = jumpPath + 'view/udwin/product/addProduct.jsp?type=addProduct';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			initProductData(1,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveLoginUser();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateLoginUser();
		});
		
		$('#add_btn').bind('click', function()
		{
			$('#ul_details').append('<li><input type="file" name="details"/><button type="button" class="btn btn-primary" onClick="clickHandler(this)">删除</button></li>');
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function addFile(e)
{
	debugger;
	var id = e.parentElement.parentElement.getAttribute("id");
	$('#'+id).append('<li><input type="file" name="'+id+'"/><button type="button" class="btn btn-primary" onClick="clickHandler(this)">删除</button></li>');
}

function clickHandler(e){
	e.parentNode.remove();
}

function editUser(product)
{
	window.location.href = jumpPath + 'view/system/user/edituser.jsp?product='+product;
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'product/deleteProduct?productUuid='+id;
		send(reqPath, "POST", "application/json", "json", "{}", function(res)
		{
			if (res.result == 'success')
			{
				initProductData(1,15);
			}
		});
	}
}

function initProductData(pageNumber,pagesize)
{
	var reqPath = basePath+'product/queryPage';
	var sendBody = '{"curPage": 1,"pageSize": 15';
	var $tab = $('#productTables tbody').html("");

	
	var reqPath = basePath + 'product/queryPage';
	var sendBody = '{"curPage": '+pageNumber+',"pageSize": '+pagesize;
	var $tab = $('#productTables tbody').html("");
	
	var requestData = $('#frm_product').serializeObject();
	requestData = JSON.stringify(requestData);
	sendBody = sendBody+requestData.replace('{',',');

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{
		if (res.result == 'success')
		{
			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '{}';
				var sum = 1;
				if(pageNumber!=1)
				{
					sum = pagesize*(pageNumber-1)+1;
				}
				$.each(res.data.data, function(i,n)
				{
//					var operation="<button onclick='editProduct(\""+n.productUuid+"\")' type='button' class='btn btn-primary'>编辑</button>"+
//								  "<button onclick='del(\""+n.productUuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					var operation= "<button onclick='del(\""+n.productUuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					rows += tpl_tr.replace(/\$num/g, sum)
								  .replace(/\$productUuid/g, n.productUuid)
							      .replace(/\$productName/,n.productName)
							      .replace(/\$locationName/, n.locationName)
							      .replace(/\$colorName/, n.colorName)
							      .replace(/\$stuffName/, n.stuffName)
							      .replace(/\$modelName/, n.modelName)
							      .replace(/\$styleName/, n.styleName)
							      .replace(/\$customCycle/, n.customCycle)
							      .replace(/\$area/, n.area)
							      .replace(/\$salesVolume/, n.salesVolume)
							      .replace(/\$price/, n.price)
							      .replace(/\$longs/, n.longs)
							      .replace(/\$wide/, n.wide)
							      .replace(/\$high/, n.high)
							      .replace(/\$wide/, n.wide)
							      .replace(/\$description/, n.description)
							      .replace(/\$status/, n.status)
							      .replace(/\$createTime/, getLocalTime(n.createTime.toString().substr(0,10)))
							      .replace(/\$updateTime/, getLocalTime((n.updateTime+"").toString().substr(0,10)))
							      .replace(/\$operation/, operation);
					sum++;
				});
				$tab.append(rows);
				// page
				pagefunction(initProductData,res.data.totalPages,pagesize);
			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

function queryLocation(locationUuid)
{
	var requsetPath = basePath + "location/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#location_uuid").append("<option value='"+n.locationUuid+"'>"+n.locationName+"</option>");
			});
			if(null!=locationUuid)
			{
				$("#location_uuid").val(locationUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryColor(colorUuid)
{
	var requsetPath = basePath + "color/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#color_uuid").append("<option value='"+n.colorUuid+"'>"+n.colorName+"</option>");
			});
			if(null!=colorUuid)
			{
				$("#color_uuid").val(colorUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryStuff(stuffUuid)
{
	var requsetPath = basePath + "stuff/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#stuff_uuid").append("<option value='"+n.stuffUuid+"'>"+n.stuffName+"</option>");
			});
			if(null!=stuffUuid)
			{
				$("#stuff_uuid").val(stuffUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryStyle(styleUuid)
{
	var requsetPath = basePath + "style/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#style_uuid").append("<option value='"+n.styleUuid+"'>"+n.styleName+"</option>");
			});
			if(null!=styleUuid)
			{
				$("#style_uuid").val(styleUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryModel(modelUuid)
{
	var requsetPath = basePath + "model/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#model_uuid").append("<option value='"+n.modelUuid+"'>"+n.modelName+"</option>");
			});
			if(null!=modelUuid)
			{
				$("#modeluuid").val(modelUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryProductType(productTypeUuid)
{
	var requsetPath = basePath + "producttype/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#productType_uuid").append("<option value='"+n.productTypeUuid+"'>"+n.productTypeName+"</option>");
			});
			if(null!=productTypeUuid)
			{
				$("#productTypeUuid").val(productTypeUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryUse(useUuid)
{
	var requsetPath = basePath + "use/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#use_uuid").append("<option value='"+n.useUuid+"'>"+n.useName+"</option>");
			});
			if(null!=useUuid)
			{
				$("#useuuid").val(modelUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function queryOpen(openUuid)
{
	var requsetPath = basePath + "open/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#open_uuid").append("<option value='"+n.openUuid+"'>"+n.openName+"</option>");
			});
			if(null!=openUuid)
			{
				$("#openuuid").val(openUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

/**
 * 
 * @METHODNAME: saveLoginUser
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveLoginUser()
{
	
	var requsetPath = basePath + "user/addUser";
	var requestData = $('#frm_login_user').serializeObject();
	requestData = JSON.stringify(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/system/user/userlist.jsp';
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}


function updateLoginUser()
{
	var requsetPath = basePath + "userManager/updateUser";
	var requestData = $('#frm_login_user').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{

		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/system/user/userlist.jsp';
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}
